package com.dsh.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {


    @Value("${guli.elasticsearch.host}")
    String host;
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        //1.连接rest接口
        String[] host_port_array = host.split(",");//127.0.0.1:9200,127.0.0.1:9201,127.0.0.1:9202
        HttpHost[] http = new HttpHost[host_port_array.length];
        for(int i= 0;i<http.length;i++){
            String[] host_port = host_port_array[i].split(":");
            http[i] = new HttpHost(host_port[0],Integer.parseInt(host_port[1]),"http");
        }
        RestClientBuilder builder= RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient=new RestHighLevelClient(builder);//高级客户端对象
        return restHighLevelClient;
    }
}
